export default function applyMiddleware(...middlewares) {

  return (createStore) => (reducer) => {
    const store = createStore(reducer);

    let dispatch = store.dispatch;
    // 我们需要加强dispatch
    let midAPI = {
      getState: store.getState,
      dispatch: (action, ...args) => dispatch(action, ...args)
    };

    // 执行中间件数组,中间件需要用到getState和displach访问我们的store
    const middlewareChain = middlewares.map((middleware) => middleware(midAPI));
    // 得到加强版的dispatch 
    dispatch = compose(...middlewareChain)(store.dispatch);

    return { ...store, dispatch };
  };
}

// 聚合函数
function compose(...funcs) {
  if (funcs.length === 0) {
    return (arg) => arg;
  }
  if (funcs.length === 1) {
    return funcs[0];
  }

  // a(b(...args))就是下一个a a是初始值或者上一次的执行结果，b是当前值
  return funcs.reduce((a, b) => (...args) => a(b(...args)));
}
